<template>
  <div>
    <el-row>
      <el-col>
        <ul class="tag-list">
          <li class="tag-item" v-for="(item, index) of listValue" :key="index">{{item.listValueName}}</li>
        </ul>
        <div v-if="showClear" class="clear-tag" @click.stop="clearAll"><fa-icon class="fa-close" /></div>
      </el-col>
    </el-row>
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <el-input clearable placeholder="请输入关键字搜索标签" v-model="dataForm.labelName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="seachList">
            <fa-icon class="fa-search" />查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row class="table-top">
      <ul class="all-tag-list">
        <template v-for="(item, index) in dataList">
          <li :class="['all-tag-item', item.isCheck ? 'active' : '']" :key="index" @click="chooseTagEvent(index, item.isCheck)">{{item.labelName}}</li>
        </template>
      </ul>
    </el-row>
    <el-row class="btn-box">
      <el-button type="" @click="close" size="small">取消</el-button>
      <el-button type="primary" @click="submit" size="small">
        <fa-icon class="fa-check" />确定</el-button>
    </el-row>
  </div>
</template>

<script>
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { mapState } from 'vuex'

export default {
  props: {
    valueList: Array
  },
  data () {
    return {
      tableLoading: false,
      showClear: false,
      dataForm: {
        labelName: '',
        pageNum: 1,
        pageSize: 999
      },
      dataList: [],
      selectedList: '',
      total: 0,
      listValue: []
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 查询列表
    getList (val) {
      console.log('查人', this.dataForm)
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.postQueryLabelData(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          if (this.valueList.length > 0) {
            this.showClear = true
            let res = this.valueList.map(ele => {
              ele.isCheck = true
              return ele
            })
            this.listValue = [...res]
            let ids = this.valueList.map(ele => ele.labelId)
            this.dataList.forEach(ele => {
              ele.isCheck = ids.includes(ele.labelId)
            })
          } else {
            this.dataList.forEach(ele => {
              ele.isCheck = false
            })
          }
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    submit () {
      let data = this.listValue
      console.log(data, '提交')
      if (data.length === 0) {
        this.$message({
          message: '请选择成员',
          type: 'warning'
        })
        return false
      }
      this.$emit('updataSuccess', data)
    },
    seachList () {
      this.getList(1)
    },
    chooseTagEvent (index, isCheck) {
      let temp = [...this.dataList]
      temp[index].isCheck = !isCheck
      let res = temp.filter(v => v.isCheck)
      res.forEach(v => {
        v.listValue = v.labelId
        v.listValueName = v.labelName
      })
      this.listValue = [...res]
      this.showClear = res.length > 0
    },
    close () {
      this.$emit('close', false)
    },
    clearAll () {
      this.listValue = []
      this.dataList.forEach(ele => {
        ele.isCheck = false
      })
      this.showClear = false
    }
  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    })
  }
}
</script>

<style lang="less">
.tag-list{
  width: 90%;
  min-height: 40px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  list-style: none;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  box-sizing: border-box;
  padding: 5px 0;
  margin-bottom: 10px;
  .tag-item{
    display: inline-block;
    box-sizing: border-box;
    padding: 2px 10px;
    font-size: 12px;
    border: 1px solid #DCDFE6;
    margin: 0 5px 5px 5px;
    line-height: 22px;
    border-radius: 4px;
  }
}
.all-tag-list{
  width: 100%;
  min-height: 40px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  list-style: none;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  box-sizing: border-box;
  padding: 5px 0;
  margin-bottom: 10px;
  .all-tag-item{
    transition: all .3s ease-in-out;
    cursor: pointer;
    display: inline-block;
    box-sizing: border-box;
    padding: 2px 10px;
    font-size: 12px;
    border: 1px solid #DCDFE6;
    margin: 0 5px 5px 5px;
    line-height: 22px;
    border-radius: 4px;
  }
}
.active{
  border: 1px solid #fff;
  background: #409EFF;
  color: #fff;
}
.btn-box{
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.clear-tag{
  position: absolute;
  right: 10px;
  top: 10px;
  z-index: 99;
  cursor: pointer;
}
</style>
